package com.wheel.autoconfigure.drools;/*
 * <h3>Title:SynologyService系统</h3>
 * <p>Copyright (c) 2022, wnfbx. All rights reserved.</p>
 * @author wnfbx
 */

import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.context.ApplicationContext;

/**
 * <dt>Description:KieSessionTemplate</dt>
 * <dd>获取Session直接使用</dd>
 *
 * @author wnfbx
 * @version 1.0 <i>2022/1/12 12:31</i>
 * @since jdk1.7+
 */
public class KieSessionTemplate {

    private final ApplicationContext applicationContext;
    public static final String DEFAULT_CONTAINER_KEY = "wDefault";

    public KieSessionTemplate(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    public KieSession getSession() {
        return getSession(DEFAULT_CONTAINER_KEY);
    }

    public KieSession getSession(String name) {
        KieContainer bean = applicationContext.getBean(name, KieContainer.class);
        if (bean == null) {
            return null;
        }
        return bean.newKieSession();
    }
}
